import json
import os.path
import random
from   conf.setting import FILE_PATH
import   yaml

def  get_testcase_yaml(file):
    try:
        with  open(file,'r',encoding='utf-8')  as  f:
            yaml_data=yaml.safe_load(f)
            return  yaml_data

    except  Exception  as  e:
        print(e)
class  Read_yamlData(object):

    def  __init__(self):
        pass
    def  write_yaml_data(self,value):
        file=None
        filepath= FILE_PATH['extract']
        if  not  os.path.exists(filepath):
            os.system(filepath)
        try:
            file=open(filepath,mode='a',encoding='utf-8')
            if  isinstance(value,dict):
                write_data=yaml.dump(value,allow_unicode=True,sort_keys=False)
                file.write(write_data)
            else:
                print('写入到extract.yaml的数据必须是字典类型格式')
        except  Exception as  e:
            print(e)

    def  get_yaml_data(self,nodename):
        fliepath=FILE_PATH['extract']
        if   os.path.exists(fliepath):
                pass
        else:
            print('extract.yaml文件不存在')
            file=open('../extract.yaml', 'w')
            file.close()
            print('extract.yaml文件创建成功')

        with  open(fliepath, 'r', encoding='utf-8')  as rf:
            extract_data=yaml.safe_load(rf)
            return   extract_data[nodename]



if __name__ == '__main__':
    # D=get_testcase_yaml('../testcase/login/Login.yaml')
    # print(D)
    # print(type(D))
    # print(type(D))
    F=Read_yamlData()
    D=F.get_yaml_data('goodsId')
    print(D)

